﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace CSharpExt.Xml
{
    public abstract class DocumentParser
    {
        public static XmlDocument Parse(String filepathOrData)
        {
            if(filepathOrData[0] == '<')
                return ParseFromData(filepathOrData);
            else
                return ParseFromFile(filepathOrData);
        }

        public static XmlDocument ParseFromFile(String filePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            return doc;
        }

        public static XmlDocument ParseFromData(String data)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(data);
            return doc;
        }

        public static XmlDocument Parse(Stream stream)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(stream);
            return doc;
        }

        public static XmlDocument Parse(TextReader reader)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            return doc;
        }


        public static XDocument XParse(String filePath)
        {
            using (Stream stream = File.OpenRead(filePath))
            {
                return XParse(new StreamReader(stream));
            }
        }

        public static XDocument XParse(Stream stream)
        {
            return XParse(new StreamReader(stream));
        }

        public static XDocument XParse(TextReader reader)
        {
            return XDocument.Load(reader);
        }
    }
}
